package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"product/models/mysql"

	"product/productrpc/internal/svc"
	"product/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderListLogic {
	return &OrderListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderListLogic) OrderList(in *productrpc.OrderListRequest) (*productrpc.OrderListResponse, error) {
	// todo: add your logic here and delete this line
	var order mysql.Order
	mysql.DB.Debug().Where("id = ?", order.ID).Find(&order)
	if order.ID == 0 {
		return nil, errors.New("当前订单已存在")
	}
	order = mysql.Order{
		Model: gorm.Model{ID: uint(in.OrderId)},
	}
	mysql.DB.Debug().Find(&order)

	return &productrpc.OrderListResponse{
		UserId:      int64(order.UserId),
		ProductId:   int64(order.ProductId),
		OrderName:   order.OrderName,
		OrderNumber: order.OrderNumber,
		OrderNum:    order.OrderNum,
	}, nil
}
